package controllers

import (
	"github.com/gin-gonic/gin"
	"hitme/go-api/logic/form"
	"hitme/go-api/logic/services"
	"hitme/go-api/modules/response"
	"hitme/go-api/modules/utils"
)

type orderController struct {

}

var OrderController orderController

// Create 创建订单
func (receiver orderController) Create(c *gin.Context)  {
	// 获取参数并校验字段
	req := form.OrderCreateReqForm{}
	if err := c.ShouldBind(&req); err != nil {
		utils.HandleValidatorError(c, err)
		return
	}

	res, err := services.OrderService.Create(c, req)

	if err != nil {
		response.Err(c, 400, 400, err.Error())
	} else {
		response.Success(c, 200, res)
	}
}

// GetList 获取订单列表
func (u *orderController) GetList (c *gin.Context)  {
   // 获取参数并校验字段
   req := form.OrderGetListReqForm{}
   if err := c.ShouldBind(&req); err != nil {
       utils.HandleValidatorError(c, err)
       return
   }

   res, err := services.OrderService.GetList(c, req)

   if err != nil {
       response.Err(c, 400, 400, err.Error())
   } else {
	   response.Success(c, 200, res)
   }
}

// GetParams 获取订单支付参数
func (u *orderController) GetParams (c *gin.Context)  {
	// 获取参数并校验字段
	req := form.OrderGetParamsReqForm{}
	if err := c.ShouldBind(&req); err != nil {
		utils.HandleValidatorError(c, err)
		return
	}

	res, err := services.OrderService.GetParams(c, req)

	if err != nil {
		response.Err(c, 400, 400, err.Error())
	} else {
		response.Success(c, 200, res)
	}
}

// Callback 回调通知
func (u *orderController) Callback (c *gin.Context)  {
	// 获取参数并校验字段
	req := form.OrderCallbackReqForm{}
	if err := c.ShouldBind(&req); err != nil {
		utils.HandleValidatorError(c, err)
		return
	}

	res, err := services.OrderService.Callback(c, req)

	if err != nil {
		response.Err(c, 400, 400, err.Error())
	} else {
		response.Success(c, 200, res)
	}
}

func (u *orderController) RefreshOrder (c *gin.Context)  {
	// 获取参数并校验字段
	req := form.OrderRefreshOrderReqForm{}
	if err := c.ShouldBind(&req); err != nil {
		utils.HandleValidatorError(c, err)
		return
	}

	res, err := services.OrderService.RefreshOrder(c, req)

	if err != nil {
		response.Err(c, 400, 400, err.Error())
	} else {
		response.Success(c, 200, res)
	}
}